﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace Foothill.Common
{
    public static class Utility 
    {
        // determine if an input string can be converted to int32
        public static bool IsNonnegativeInt32(string str)
        {
            int tmp = 0;

            if (Int32.TryParse( str, out tmp ))
            {
                return ( tmp >= 0 );
            }
            else
            {
                return false;
            }
        }

        // from current year back to 1990
        public static Dictionary<int, string> GetYearDict()
        {
            Dictionary<int, string> result = new Dictionary<int, string>();

            for (int i = DateTime.Now.Year; i >= GlobalSettings.StartYear; --i)
            {
                result.Add(i, i.ToString());
            }

            return result;
        }

        // get a file name whose name is a Guid and whose extension is the same as the filename passed in
        public static string GetGuidFileName(string filename)
        {
            Guid guid = System.Guid.NewGuid();
            return guid.ToString() + Path.GetExtension(filename);
        }

        // flip the sort direction
        public static fhSortDirection FlipSortDirection(fhSortDirection direction)
        {
            return direction == fhSortDirection.ASC ? fhSortDirection.DESC : fhSortDirection.ASC;
        }

        public static void GetPriceRange(PriceRange priceRange, out int priceFrom, out int priceTo )
        {
            priceFrom = 0;
            priceTo = 0;
        }

        public static int ConvertToInt(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return 0;
            }

            int result;
            if (int.TryParse(str, out result))
            {
                return result;
            }

            return 0;
        }

        public static string Truncate(this string value, int maxLength)
        {
            return value.Length <= maxLength ? value : value.Substring(0, maxLength);
        }
    }
}
